package com.ecoolex.ying.space.api.graphql;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.ying.space.entity.MarktingWholeHouseCase;
import com.ecoolex.ying.space.entity.OutsourcingPackaged;
import com.ecoolex.ying.space.entity.WholeHouseCase;
import com.ecoolex.ying.space.entity.WholeHouseCaseItems;
import com.ecoolex.ying.space.ws.OutsourcingGoodsService;
import com.ecoolex.ying.space.ws.WholeHouseCaseService;


@GraphQL2Module(type = Type.User, description = "整屋案例营销")
public class MarktingWholeHouseCaseGraphql {

    @Autowired
    private WholeHouseCaseService wholeHouseCaseService;

    @Autowired
    private StorageService storageService;

    @Autowired
    private OutsourcingGoodsService outsourcingGoodsService;

    @Autowired
    private ConsumerMemberManager consumerMemberManager;

    @TypeField(type = WholeHouseCase.class, field = "releaseUser", description = "发布用户")
    public ConsumerMember getCommentUser(@Source WholeHouseCase source) {
        return consumerMemberManager.getByUserId(source.getUserId());
    }

    @TypeField(type = WholeHouseCase.class, field = "totalRetailPrice", description = "案例包含套餐总价格")
    public BigDecimal totalRetailPrice(@Source WholeHouseCase source) {
        return outsourcingGoodsService.getTotalRetailPriceByPackagedId(source.getCaseId());
    }

    @TypeField(type = MarktingWholeHouseCase.class, field = "wholeHouseCase", description = "整屋案例")
    public WholeHouseCase wholeHouseCase(@Source MarktingWholeHouseCase source) {
        return wholeHouseCaseService.getWholeHouseCaseById(source.getCaseId());
    }

    @TypeField(type = WholeHouseCase.class, field = "coverImage", description = "案例封面")
    public StorageInfo coverImage(@Source WholeHouseCase source) {
        return storageService.get(source.getCoverImage());
    }

    @TypeField(type = WholeHouseCaseItems.class, field = "images", description = "案例栏目图片")
    public List<StorageInfo> images(@Source WholeHouseCaseItems source) {
        JSONArray array = JSONArray.parseArray(source.getImages());
        List<String> storageIdLsit = array.toJavaList(String.class);
        return storageIdLsit.stream().map(storageId -> storageService.get(storageId)).collect(Collectors.toList());
    }

    @TypeField(type = WholeHouseCase.class, field = "wholeHouseCaseItemsList", description = "案例栏目列表")
    public List<WholeHouseCaseItems> itemList(@Source WholeHouseCase source) {
        return wholeHouseCaseService.getWholeHouseCaseItemsByCaseId(source.getCaseId());
    }

    @TypeField(type = WholeHouseCaseItems.class, field = "hasOtherPackaged", description = "有无其他套餐")
    public boolean hasOtherPackaged(@Source WholeHouseCaseItems source) {
        return wholeHouseCaseService.countByItemsName(source.getName()) > 1 ? true
            : false;
    }

    @TypeField(type = WholeHouseCaseItems.class, field = "outsourcingPackaged", description = "外购套餐")
    public OutsourcingPackaged outsourcingPackaged(@Source WholeHouseCaseItems source) {
        return outsourcingGoodsService.getOutsourcingPackagedById(source.getOutsourcingPackagedId());
    }

    @TypeField(type = WholeHouseCaseItems.class, field = "wholeHouseCase", description = "所属案例")
    public WholeHouseCase wholeHouseCase(@Source WholeHouseCaseItems source) {
        return wholeHouseCaseService.getWholeHouseCaseById(source.getCaseId());
    }

    @QueryField(field = "wholeHouseCase", description = "整屋案例详情")
    public WholeHouseCase getWholeHouseCase(@NonNull Integer caseId) {
        return wholeHouseCaseService.getWholeHouseCaseByIdForH5(caseId);
    }

    @QueryField(field = "marktingWholeHouseCase", description = "整屋案例营销详情")
    public MarktingWholeHouseCase get(@NonNull Integer id) {
        return wholeHouseCaseService.getMarktingWholeHouseCaseByIdForH5(id);
    }

    @QueryField(field = "marktingWholeHouseCaseList", description = "整屋案例营销列表")
    public Pagination<MarktingWholeHouseCase> query(String name, @NonNull Sorting[] sorts, @NonNull Page page) {
        return wholeHouseCaseService.queryMarktingWholeHouseCaseForH5(name, sorts, page);
    }

    @QueryField(field = "styleDescriptionList", description = "案例风格列表")
    public Pagination<String> queryStyleDescription(@NonNull String itemsName, @NonNull Page page) {
        return wholeHouseCaseService.queryStyleDescriptionByItemsName(itemsName, page);
    }

    @QueryField(field = "associatedWholeHouseCaseItemsList", description = "关联套餐案例栏目列表")
    public Pagination<WholeHouseCaseItems> queryWholeHouseCaseItems(@NonNull String itemsName, @NonNull String styleDescription, @NonNull Page page) {
        return wholeHouseCaseService.queryByItemsNameAndStyleDescription(itemsName, styleDescription, page);
    }
}
